package org.autoparser.exttree;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Vector;

import org.autoparser.exttree.exception.ZOddPageException;

public class ZDataContainer {
	private HashMap<String,HashMap<String, Vector<String>>> dataMaps;
	public class PairIterator implements Iterator<Vector<String>>{
		private int index = 0;
		private HashMap<String, Vector<String>> dataMap;
		private Vector<String> keys;
		public PairIterator(final String key) throws ZOddPageException{
			index = 0;
			dataMap = dataMaps.get(key);
			if(dataMap == null)
				throw new IllegalArgumentException("wrong arg: " + key);
			keys = new Vector<String>();
			keys.addAll(dataMap.keySet());
			if(keys.size() == 0)
				throw new ZOddPageException();
		}
		public String getKey(int index){
			return keys.elementAt(index);
		}
		public int getIndex(String key){
			for(int i = 0; i < keys.size(); ++i)
				if(keys.elementAt(i).compareToIgnoreCase(key) == 0)
					return i;
			return -1;
		}
		@Override
		public boolean hasNext() {
			for(String key: keys){
				if(dataMap.get(key).size() <= index)
					return false;
			}
			return true;
		}

		@Override
		public Vector<String> next() {
			Vector<String> vct = new Vector<String>();
			for(String key: keys){
				vct.add(dataMap.get(key).elementAt(index));
			}
			++index;
			return vct;
		}
		@Override
		public void remove() {
		}
	};
	public ZDataContainer(){
		dataMaps = new HashMap<String,HashMap<String, Vector<String>>>();
	}
	public void clear(){
		for(HashMap<String, Vector<String>> map: dataMaps.values())
			map.clear();
	}
	public HashMap<String, Vector<String>> getSubContainer(String key){
		HashMap<String, Vector<String>> sc = dataMaps.get(key);
		if(sc == null){
			sc = new HashMap<String, Vector<String>>();
			dataMaps.put(key, sc);
		}
		return sc;
	}
	public Vector<String> getColumns(String key){
		if(dataMaps.get(key) == null)
			return null;
		Vector<String> cols = new Vector<String>();
		cols.addAll(dataMaps.get(key).keySet());
		return cols;
	}
	public PairIterator getIterator(String key) throws ZOddPageException{
		if(dataMaps.get(key) == null)
			return null;
		return new PairIterator(key);
	}
}
